<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 29/08/2017
 * Time: 17:36
 */
namespace backend\controllers;

use common\models\ar\User;
use common\models\Common;
use common\models\Sms;
use backend\models\OnlyCaptchaValidate;
use yii\web\Controller;

class PasswordController extends Controller
{
    public $layout = 'login';

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'common\actions\CaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
                'transparent' => true,
                'offset' => 8,
                'padding' => 1,
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->redirect('retrieve');
    }

    public function actionRetrieve()
    {
        return $this->render('index');
    }

    public function actionRetrieveSubmit()
    {
        $req = \Yii::$app->request;
        $password = trim($req->post("password", ''));
        $passwordConfirm = trim($req->post('password_confirm', ''));
        $captcha = trim($req->post("captcha", ''));
        $clientSN = trim($req->post("clientSN", ''));

        $validate = new OnlyCaptchaValidate();
        $validate->controller = 'password';
        $validate->captcha = $captcha;

        if (!$validate->validate()) {
            exit(json_encode(['code' => 1001, 'msg' => "验证码不正确 "]));
        }

        $cellphone = trim($req->post("cellphone", ''));
        $check = Common::dataCheck($cellphone, [
            'type' => 3,
            'detail' => [
                'pattern' => '/^1[34578][0-9]{9}$/',
                'info' => '手机号码不正确'
            ],
        ]);
        if ($check['code'] != 1) {
            $code = 1003;
            $msg = $check['msg'];
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $obj = User::find()->where(["clientSN" => $clientSN])->one();
        if (!$obj) {
            $code = 1004;
            $msg = "账户不存在";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        if ($obj->cellphone != $cellphone) {
            $code = 1004;
            $msg = "账户绑定的手机号码不正确";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        if (strlen($password) < 6) {
            $code = 1003;
            $msg = '密码长度不能小于6位';
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $smsCode = trim($req->post('smsCode', ''));
        $check = Sms::smsCodeCheck($cellphone, $smsCode, 4);
        if ($check['code'] != 1) {
            $code = 1005;
            $msg = $check['msg'];
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $newPassword = md5(md5($password) . $obj->password_salt);
        if ($obj->password == $newPassword) {
            $code = 1006;
            $msg = "未修改 | 新旧密码相同";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }
        $obj->password = $newPassword;
        if ($obj->save()) {
            $code = 1;
            $msg = '重置成功';
        } else {
            $code = 0;
            $msg = '重置失败';
        }

        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }

    /**
     * 发送短信
     */
    public function actionSendSms()
    {
        $cellphone = trim(\Yii::$app->request->get('cellphone', ''));
        $captcha = trim(\Yii::$app->request->get('captcha', ''));
        $clientSN = trim(\Yii::$app->request->get('clientSN', ''));
        if (!$clientSN) {
            exit(json_encode(['code' => 1001, 'msg' => "请输入要找回密码的代理编号 "]));
        }

        $validate = new OnlyCaptchaValidate();
        $validate->controller = 'password';
        $validate->captcha = $captcha;

        if (!$validate->validate()) {
            exit(json_encode(['code' => 1002, 'msg' => "验证码不正确 "]));
        }

        $check = Common::dataCheck($cellphone, [
            'type' => 3,
            'detail' => [
                'pattern' => '/^1[34578][0-9]{9}$/',
                'info' => '手机号码不正确'
            ],
        ]);
        if ($check['code'] != 1) {
            $code = 1003;
            $msg = $check['msg'];
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $obj = User::find()->where(["clientSN" => $clientSN])->one();
        if (!$obj) {
            $code = 1004;
            $msg = "账户不存在";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        if ($obj->cellphone != $cellphone) {
            $code = 1004;
            $msg = "账户绑定的手机号码不正确";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }
        $return = Sms::send($cellphone,4);
        exit(json_encode($return));
    }
}